package com.aeye.mbr.upms.rpc.service.impl;

import com.aeye.mbr.common.result.BootstrapTableParams;
import com.aeye.mbr.common.result.BootstrapTableResult;
import com.aeye.mbr.common.plugin.mapper.BaseServiceImpl;
import com.aeye.mbr.upms.rpc.mapper.UpmsUserRoleMapper;
import com.aeye.mbr.upms.dao.model.UpmsUserRole;
import com.aeye.mbr.upms.rpc.api.UpmsUserRoleService;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

/**
* UpmsUserRoleService实现
* Created by shuaeye.mbr on 2017/3/20.
*/
@Service
@Transactional
public class UpmsUserRoleServiceImpl extends BaseServiceImpl<UpmsUserRole> implements UpmsUserRoleService {

    private static Logger _log = LoggerFactory.getLogger(UpmsUserRoleServiceImpl.class);

    @Autowired
    UpmsUserRoleMapper upmsUserRoleMapper;

    @Override
    public int role(String[] roleIds, int id) {
        int result = 0;
        // 删除旧记录
        Example upmsUserRoleExample = new Example(UpmsUserRole.class);
        upmsUserRoleExample.createCriteria()
                .andEqualTo("userId",id);
        upmsUserRoleMapper.deleteByExample(upmsUserRoleExample);
        // 增加新记录
        if (null != roleIds) {
            for (String roleId : roleIds) {
                if (StringUtils.isBlank(roleId)) {
                    continue;
                }
                UpmsUserRole upmsUserRole = new UpmsUserRole();
                upmsUserRole.setUserId(id);
                upmsUserRole.setRoleId(NumberUtils.toInt(roleId));
                result = upmsUserRoleMapper.insertSelective(upmsUserRole);
            }
        }
        return result;
    }

    @Override
    public BootstrapTableResult<UpmsUserRole> selectUpmsUserRoleByParams(BootstrapTableParams params) {
        return null;
    }
}